﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Draco.Studio.Core
{
    public class ServiceContext:IServiceContext
    {
        private string m_ContextUID = Guid.NewGuid().ToString();
        private string m_AppBasePath = AppDomain.CurrentDomain.BaseDirectory;
        private string m_RelativeBundlePath = "Plugins";

        private List<String> m_ServiceNames = new List<string>();
        private IDictionary<String, IService> m_ServicesObject = new Dictionary<String, IService>();

        /// <summary>
        /// constructor
        /// </summary>
        public ServiceContext()
        {
 
        }

        #region ContextPath
        public string ContextUID
        {
            get { return m_ContextUID; }
        }

        public string AppBasePath
        {
            get { return m_AppBasePath; }
        }

        public string AbsoluteBundlePath
        {
            get { return System.IO.Path.Combine(m_AppBasePath, m_RelativeBundlePath); }
        }

        public string RelativeBundlePath
        {
            get { return m_RelativeBundlePath; }
        }
        #endregion

        public List<IService> GetServices()
        {
            List<IService> list = new List<IService>();
            if (m_ServiceNames != null && m_ServiceNames.Count > 0)
            {
                foreach (String name in m_ServiceNames)
                {
                    list.Add(m_ServicesObject[name]);
                }
            }
            return list;
        }

        public IService GetService(string ServiceName)
        {
            if (!String.IsNullOrEmpty(ServiceName) && m_ServiceNames.Contains(ServiceName))
                return m_ServicesObject[ServiceName];
            return null;
        }

        public bool RegisterService(string ServiceName, IService service)
        {
            if (String.IsNullOrEmpty(ServiceName) || service == null)
                throw new ArgumentException();
            if (m_ServiceNames.Contains(ServiceName))
            {
                m_ServicesObject[ServiceName] = service;
            }
            else
            {
                m_ServiceNames.Add(ServiceName);
                m_ServicesObject.Add(ServiceName, service);
            }
            return true;
        }

        public void UnRegisterService(string ServiceName, IService service)
        {
            if (String.IsNullOrEmpty(ServiceName) || service == null)
                throw new ArgumentException();
            if (m_ServiceNames.Contains(ServiceName))
            {
                IService svcObject = m_ServicesObject[ServiceName];
                if (service.Equals(svcObject))
                {
                    m_ServiceNames.Remove(ServiceName);
                    m_ServicesObject.Remove(ServiceName);
                }
                else
                {
                    throw new Exception("The register service object doesn't match the unregister service Object!");
                }
            }
        }
    }
}
